<template>
  <div>
    <a-modal v-model="visible" title="添加职位" :confirm-loading="confirmLoading" @ok="handleOk" @cancel="$emit('update:visible', false)">
      <a-form :form="form" :label-col="{ span: 4 }" :wrapper-col="{ span: 18 }">
        <a-form-item label="职位">
          <a-select v-decorator="['recruitInfoId', { rules: [{ required: true, message: '职位不能为空' }] }]">
            <a-select-option :value="item.id" v-for="item in tableData" :key="item.id">
              {{ item.postName }}
            </a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="阶段">
          <a-select v-decorator="['status', { rules: [{ required: true, message: '阶段不能为空' }] }]">
            <a-select-option :value="1"> 已投递 </a-select-option>
            <a-select-option :value="2"> 已约面 </a-select-option>
            <a-select-option :value="3"> 待入职 </a-select-option>
            <a-select-option :value="4"> 已招聘 </a-select-option>
            <a-select-option :value="5"> 未通过 </a-select-option>
          </a-select>
        </a-form-item>
      </a-form>
    </a-modal>
    <a-drawer
      title="投递岗位"
      placement="right"
      :closable="false"
      width="500px"
      :visible="visibleDeliveringJobs"
      :destroyOnClose="true"
      @close="onCloseDeliveringJobs"
    >
      <ShowResume :status="'sendResume'" :companyUnitsInfo="{ ...peopleInfo }" @sendResume="sendResumeFun" @handleSubmit="handleSubmit" />
    </a-drawer>
  </div>
</template>

<script>
import moment from "moment";
import ShowResume from "@/views/recruitment/talent/msg/component/showResume/index.vue";
import { getRecruitInfos, postDeliverRecords, putDeliverRecords, deliverRecordsAddJob } from "../api";
export default {
  components: { ShowResume },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    type: {
      type: String,
      default: "add"
    },
    id: {
      type: String,
      default: ""
    },
    statusP: {
      type: Number,
      default: 1
    },
    talentPoolId: {
      type: String,
      default: ""
    },
    deliverRecordId: {
      type: String,
      default: ""
    }
  },
  watch: {
    visible(v) {
      if (v) {
        if (this.id) {
          setTimeout(() => {
            this.form.setFieldsValue({
              recruitInfoId: this.id,
              status: this.statusP >= 5 ? 5 : this.statusP
            });
          }, 100);
        } else {
          this.form.resetFields();
        }
      }
    }
  },
  data() {
    return {
      peopleInfo: {},
      visibleDeliveringJobs: false,
      confirmLoading: false,
      form: this.$form.createForm(this, {}),
      tableData: []
    };
  },
  created() {
    this.onGetRecruitInfos();
  },
  methods: {
    sendResumeFun() {
      this.visibleDeliveringJobs = false;
    },
    moment,
    async onGetRecruitInfos() {
      try {
        this.spinning = true;
        this.visible = false;
        const { data } = await getRecruitInfos({
          pageNo: 1,
          pageSize: 1000,
          isCancel: 2,
          "orders[0].property": "gmtCreate",
          "orders[0].desc": true
        });
        this.tableData = data.entities;
        setTimeout(() => (this.spinning = false), 500);
      } catch (err) {
        console.log("error log --> ", err);
        this.tableData = [];
        setTimeout(() => (this.spinning = false), 500);
      }
    },
    fun(v) {
      if (this.type === "add") {
        v.activeDelivery = false;
        v.saveType = 1;
        return postDeliverRecords(v);
      } else {
        v.id = this.deliverRecordId;
        return putDeliverRecords(v);
      }
    },
    async onPostDeliverRecords(v) {
      try {
        if (v.status === 2) {
          this.$emit("upDate", v.status, "", v);
        } else {
          this.confirmLoading = true;
          const { data } = await this.fun(v);
          this.confirmLoading = false;
          this.$notification.success({ message: "操作成功" });
          this.$emit("update:visible", false);
          if (this.type === "add") {
            this.$emit("upDate", v.status, data.id);
          } else {
            this.$emit("upDate", v.status, this.deliverRecordId);
          }
        }
      } catch (err) {
        console.log("error log --> ", err);
        this.confirmLoading = false;
      }
    },
    onCloseDeliveringJobs() {
      this.visibleDeliveringJobs = false;
    },
    handleSubmit(val) {
      console.log(val);
      if (val.workExperience && val.workExperience.length < 10) {
        this.$message.error("工作经历最少输入10个字！");
        return;
      } else if (val.workExperience && val.workExperience.length > 520) {
        this.$message.error("工作经历最多输入520个字！");
        return;
      }
      val.workYears = val.workYears && val.workYears === 0 ? 0 : val.workYears;
      var s = true;
      if (val.skill) {
        if (val.skill.length > 3) {
          this.$message.error("擅长技能最多只能填三个");
          s = false;
        }
        val.skill.some((e) => {
          if (e.length > 6) {
            this.$message.error("每个标签长度不能超过6位");
            s = false;
            return true;
          }
        });
      }
      if (s) {
        val.skill = val.skill ? val.skill.join(",") : "";
        const data = [];
        const Info = { ...val };
        Object.keys(Info).map((res) => {
          data.push({ enName: res, content: Info[res] });
        });
        deliverRecordsAddJob({
          status: this.peopleInfo.status,
          recruitInfoId: this.peopleInfo.recruitInfoId,
          talentPoolId: this.peopleInfo.talentPoolId,
          formList: data
        }).then((res) => {
          if (res.data.requiredFields) {
            this.$notification.error({ message: res.data.requiredFields });
          } else {
            if (this.peopleInfo.status === 2) {
              this.$emit("upDate", this.peopleInfo.status, "", this.peopleInfo);
            } else {
              this.visibleDeliveringJobs = false;
              this.$notification.success({ message: "操作成功" });
              this.$emit("update:visible", false);
              if (this.type === "add") {
                this.$emit("upDate", this.peopleInfo.status, res.data.id);
              } else {
                this.$emit("upDate", this.peopleInfo.status, this.deliverRecordId);
              }
            }
          }
        });
      }
    },
    handleOk() {
      this.form.validateFields((err, values) => {
        if (!err) {
          values.talentPoolId = this.talentPoolId;
          values.id = this.talentPoolId;
          values.updateStatus = 1;
          this.$emit("update:visible", false);
          this.peopleInfo = values;
          this.visibleDeliveringJobs = true;
        }
      });
    }
  }
};
</script>

<style></style>
